﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using Sern.Common.ExpressionSerializer;

namespace Sern.Common
{
    public class DispatchDeserializingParameterInspector : IParameterInspector
    {
        /// <summary>
        /// Deserialize any serialized expression.
        /// </summary>
        /// <param name="operationName"></param>
        /// <param name="inputs"></param>
        /// <returns></returns>
        public object BeforeCall(string operationName, object[] inputs)
        {
            for (int i = 0; i < inputs.Length; i++)
            {
                if (inputs[i] is SerializedExpression)
                {
                    inputs[i] =
                        ExpressionSerializerFactory.Create.Deserialize(
                            (inputs[i] as SerializedExpression).XmlExpression);
                }
            }

            return Guid.NewGuid();
        }

        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState){}
    }
}
